package lt.marsrutai.mobile.android.activity.handler;

import java.util.ArrayList;
import java.util.List;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.fragment.NameDialogFragment;
import lt.marsrutai.mobile.android.handler.menu.StandartMenuHandler;
import lt.marsrutai.mobile.android.model.FastestRouteSearchBookmark;
import lt.marsrutai.mobile.android.util.BookmarkControlManager.BookmarkInfoProvider;
import lt.marsrutai.mobile.android.util.StringUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;

import com.actionbarsherlock.view.MenuItem;

/**
 * Menu handler for activities providing fastest route search bookmarks
 * 
 * @author Justas
 */
public class FastestRouteSearchBookmarkMenuHandler extends StandartMenuHandler {
    
    private BookmarkInfoProvider<FastestRouteSearchBookmark> bookmarkProvider;

    public FastestRouteSearchBookmarkMenuHandler(BookmarkInfoProvider<FastestRouteSearchBookmark> bookmarkProvider) {
        this.bookmarkProvider = bookmarkProvider;
    }
    
    @Override
    public Intent handle(MenuItem item, Activity activity) {
        switch (item.getItemId()) {
        case R.id.bookmark:
            DialogFragment dialog = new NameDialogFragment();
            Bundle bundle = new Bundle();
            FastestRouteSearchBookmark bookmark = bookmarkProvider.getBookmark();

            List<String> parts = new ArrayList<String>();
            String start = bookmark.getStart();
            if (!StringUtils.isEmpty(start)) {
                parts.add(start);
            }
            String end = bookmark.getEnd();
            if (!StringUtils.isEmpty(end)) {
                parts.add(end);
            }
            
            bundle.putString(NameDialogFragment.INITIAL_VALUE, StringUtils.join(parts, " - "));

            dialog.setArguments(bundle);
            dialog.show(((FragmentActivity) activity).getSupportFragmentManager(), "BookmarkNameDialogFragment");
            break;
        default:
            return super.handle(item, activity);
        }
        return null;
    }
};
